package com.wwg.market.ui.code.client;


import com.mobileares.midp.widgets.client.page.ItemModel;
import com.mobileares.midp.widgets.client.page.MenuBuilder;
import com.mobileares.midp.widgets.client.page.MenuModel;
import com.mobileares.midp.widgets.client.page.PageModel;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.wwfish.gwt.model.client.ILoadModelProvider;
import com.wwfish.gwt.model.client.IModelCallback;
import com.wwfish.gwt.model.client.impl.FishModelManager;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.service.interfaces.CodeService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author gaoyang
 * @version $Id: $
 * @date 11-12-15 下午10:46
 */
public class MenuImpl implements MenuBuilder {
    public List<MenuModel> getMenu() {
        List<MenuModel> result = new ArrayList<MenuModel>();
        MenuModel menu = new MenuModel("数据管理", MenuImpl.class.getName());
        menu.setPages(getPageModels(menu));
        result.add(menu);
        menu.addCacheCallBack(new ItemModel.CacheDateCallBack() {
            public void process() {
                //todo
            }
        });
        initModel();

        return result;
    }

    private List getPageModels(MenuModel menu) {
        PageModel page = new PageModel();
        page.setMenuModel(menu);
        page.setPageId(MenuImpl.class.getName());
        page.setPageTitle("代码表管理");

        List r = new ArrayList();
        PageModel page1 = new PageModel();
        page1.setMenuModel(menu);
        page1.setPageId(CodeManagerPage.class.getName());
        page1.setPageTitle("代码表维护");

        r.add(page1);

        List result = new ArrayList();
        page.setChildren(r);
        result.add(page);
        return result;
    }

    private void initModel() {
        FishModelManager.createAppModel("codes", FishModelManager.TYPE_LIST, new ILoadModelProvider() {
            public void getModel(Object[] parameters, final IModelCallback iModelCallback) {
                Utils.invoke(CodeService.class.getName(), "getRootGeneralCodes", null, new LoadingAsyncCallback() {
                    @Override
                    protected void success(Object result) {
                        iModelCallback.setFishModel(result);
                    }
                });
            }
        }).loadByProvider(null);
    }
}
